package cn.com.darly.videoandroidpm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.clearedit.ClearEditText;
import com.darly.dview.widget.header.TitleView;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.ui.biz.RegisterNextListener.RegisterNextInterface;
import cn.com.darly.videoandroidpm.ui.presenter.RegisterNextPresenter;

/** 用户注册页面
 * @author Darly/张宇辉/2018/4/25 15:09
 * @version 1.0/cn.com.darly.videoandroidpm.ui
 */
@ContentBinder(R.layout.activity_register_next)
public class RegisterNextActivity extends BaseActivity implements OnClickListener,RegisterNextInterface {
    @ViewsBinder(R.id.id_base_title)
    public TitleView id_base_title;
    @ViewsBinder(R.id.id_register_next_anim)
    LinearLayout id_register_next_anim;

    @ViewsBinder(R.id.id_register_next_account)
    ClearEditText id_register_next_account;
    @ViewsBinder(R.id.id_register_next_pwd)
    ClearEditText id_register_next_pwd;

    private String mobile;
    RegisterNextPresenter presenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        //添加登录界面动画效果
        id_register_next_anim.setAnimation(AnimationUtils.loadAnimation(this,R.anim.login_up));
        int res =  getIntent().getIntExtra("title",0);
        if (res == 0) {
            id_base_title.setTitle(R.string.app_name);
        }else {
            id_base_title.setTitle(res);
        }
        id_base_title.removeBackground(R.drawable.ic_title_background);

        mobile =  getIntent().getStringExtra("tel");
        if (TextUtils.isEmpty(mobile)){
            ToastApp.showToast("系统错误，参数传递错误");
        }
        presenter = new RegisterNextPresenter(this);
    }

    @Override
    protected void loadData() {
        SpannableString nams = new SpannableString("请输入用户名");//定义hint的值
        AbsoluteSizeSpan namass = new AbsoluteSizeSpan(16, true);//设置字体大小 true表示单位是sp
        nams.setSpan(namass, 0, nams.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        id_register_next_account.setHint(new SpannedString(nams));
        SpannableString codes = new SpannableString("请输入密码");//定义hint的值
        AbsoluteSizeSpan codeser = new AbsoluteSizeSpan(16, true);//设置字体大小 true表示单位是sp
        nams.setSpan(codeser, 0, codes.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        id_register_next_pwd.setHint(new SpannedString(codes));
    }

    @Override
    protected void initListener() {
        id_base_title.setLeftBackOneListener(R.mipmap.ic_title_back,this);
        id_base_title.setRightViewRightTextOneListener("注册",this);
    }


    @Override
    public void onClick(View v) {
        presenter.onClickDown(this,v);
    }

    @Override
    public void onRegisterSuccess(LoginInfo info) {
        Intent intent = new Intent(this,LoginActivity.class);
        intent.putExtra("username",findAccount());
        intent.putExtra("password",findPwd());
        intent.putExtra("autologin",true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        onBackPressed();
    }

    @Override
    public void onRegisterFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public void onCheckUserSuccess(String code) {
        presenter.register();
    }

    @Override
    public void onCheckUserFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public String findAccount() {
        return id_register_next_account.getText().toString().trim();
    }

    @Override
    public String findPwd() {
        return id_register_next_pwd.getText().toString().trim();
    }

    @Override
    public String findTel() {
        return mobile;
    }

    @Override
    public void end() {
        onBackPressed();
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }
}
